package com.wuwenze.excelkitexamples.rest;

import com.google.common.collect.ImmutableMap;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;

import com.wuwenze.excelkitexamples.entity.UserForAnno;
import com.wuwenze.excelkitexamples.entity.UserForXml;
import com.wuwenze.excelkitexamples.entity.UserGroup;
import com.wuwenze.poi.ExcelKit;
import com.wuwenze.poi.annotation.Excel;
import com.wuwenze.poi.handler.ExcelReadHandler;
import com.wuwenze.poi.pojo.ExcelErrorField;

import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletResponse;

@RestController
public class UserExcelRestApi {


    // 使用 xml 配置方式使用 ExcelKit
    @GetMapping("/down_template_by_xml")
    public void downTemplate(HttpServletResponse response) {
        // 构造演示数据
        List<UserForXml> exampleRows = Lists.newArrayList(//
                UserForXml.builder()
                        .name("姓名")
                        .sex(1)
                        .tel("17311223344")
                        .createAt(new Date())
                        .userGroup(UserGroup.builder().name("研发部").build())
                        .field1("121233")
                        .field2(0)
                        .build()
        );

        // 构建并下载模板
        ExcelKit.$Export(UserForXml.class, response).downXlsx(exampleRows, true);
    }

    // 使用注解配置的方式使用 ExcelKit
    @GetMapping("/down_template_by_anno")
    public void downTemplate2(HttpServletResponse response) {
        // 构造演示数据
        List<UserForAnno> exampleRows = Lists.newArrayList(//
                UserForAnno.builder()
                        .name("姓名")
                        .sex(1)
                        .tel("17311223344")
                        .createAt(new Date())
                        .userGroup(UserGroup.builder().name("研发部").build())
                        .field1("121233")
                        .field2(0)
                        .build()
        );

        // 构建并下载模板
        ExcelKit.$Export(UserForAnno.class, response).downXlsx(exampleRows, true);
    }


    @PostMapping("/import")
    public ResponseEntity<?> importExcel(@RequestParam("excelFile") MultipartFile file) throws IOException {
        // 省略文件检查..

        // 执行文件导入.
        long beginTimeMillis = System.currentTimeMillis();
        final List<UserForAnno> data = Lists.newArrayList();
        final List<Map<String, Object>> error = Lists.newArrayList();
        ExcelKit.$Import(UserForAnno.class)//
                .readXlsx(file.getInputStream(), new ExcelReadHandler<UserForAnno>() {
                    @Override
                    public void onSuccess(int sheet, int row, UserForAnno userForAnno) {
                        // 当前行读取成功, 入库或加入批量入库队列.
                        data.add(userForAnno);
                    }

                    @Override
                    public void onError(int sheet, int row, List<ExcelErrorField> errorFields) {
                        // 当前行读取失败, 获取失败详情.
                        error.add(ImmutableMap.of("row", row, "errorFields", errorFields));
                    }
                });
        long time = ((System.currentTimeMillis() - beginTimeMillis) / 1000L);
        System.out.println("数据量: " + data.size() + ", 耗时: " + time + "秒");
        ImmutableMap<String, Object> retJsonMap = ImmutableMap.of(//
                "time", "耗时" + time + "秒",
                "data", data,
                "error", error
        );
        return ResponseEntity.ok(retJsonMap);
    }

    @GetMapping("/export")
    public void export(HttpServletResponse response) {
        List<UserForAnno> rows = Lists.newArrayList();
        for (int i = 0; i < 100000; i++) {
            UserForAnno build = UserForAnno.builder()
                    .name("name." + i)
                    .sex(i % 2 == 0 ? 1 : 2)
                    .tel("17311223344")
                    .createAt(new Date())
                    .userGroup(UserGroup.builder().name("研发部").build())
                    .field1("121233")
                    .field2(0)
                    .build();
            rows.add(build);
        }
        long beginTimeMillis = System.currentTimeMillis();
        ExcelKit.$Export(UserForAnno.class, response).downXlsx(rows, false);
        long time = ((System.currentTimeMillis() - beginTimeMillis) / 1000L);
        System.out.println("数据量: " + rows.size() + ", 耗时: " + time + "秒");
    }
}
